package com.safe.text;

import com.safe.text.R;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;

public class SplashActivity extends BaseActivity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		final Intent mainService = new Intent(SplashActivity.this,
				MainService.class);
		currentActivity = this;

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		if (!prefs.contains(KEY_FIRST_RUN)) {
			/* do some one-off stuff here */
			prefs.edit().putLong(KEY_FIRST_RUN, System.currentTimeMillis())
					.commit();
		}
		phoneNumber = prefs.getString("phoneNumber", "");
		emailAddress = prefs.getString("emailAddress", "");
		password = prefs.getString("password", "");
		// Start the service if the service did not execute onDestroy
		if (prefs.getBoolean("serviceStatus", false)) {
			if (!isMyServiceRunning()) {
				startService(mainService);
			}
		}

		// if (phoneNumber == "" && emailAddress == "" && password == "") {
		// splash = new Intent(SplashActivity.this, LaunchActivity.class);
		// } else {
		// if (UserId == ""){
		// getUserObjectId(emailAddress, password);
		// }
		// splash = new Intent(SplashActivity.this, MainActivity.class);
		// }
		final boolean redirect = isLoggedIn();
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			public void run() {
				Intent splash;
				if (redirect) {
					splash = new Intent(SplashActivity.this, MainActivity.class);
				} else {
					splash = new Intent(SplashActivity.this,
							LaunchActivity.class);
				}
				finish();
				startActivity(splash);
			}
		}, 1000);
	}

	public void onResume() {
		currentActivity = this;
		super.onResume();
	}
}
